The SELECT Clause
In this lesson, we will take a look at the SELECT statement.
We'll cover the following
The SELECT clause#
The SQL SELECT statement is used to fetch the data from a database table that returns this data in the form of a result table. These result tables are called result-sets.
Syntax#
The basic syntax of the SELECT
statement is as follows:
SELECT column1, column2, ... columnN FROM table_name;
Here, SELECT
specifies the column1, column2… to be selected and the FROM
clause specifies from which table these columns are to be selected.
If you want to fetch all the fields available in the table, then you can use the following syntax:
SELECT * FROM table_name;
Example#
Consider the CUSTOMERS table we used in the last lesson:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Mark | 32 | Texas | 50000.00 |
2 | John | 25 | NY | 65000.00 |
3 | Emily | 23 | Ohio | 20000.00 |
4 | Bill | 25 | Chicago | 75000.00 |
5 | Tom | 27 | Washington | 35000.00 |
6 | Jane | 22 | Texas | 45000.00 |
Let’s say we want to fetch the ID
, Name
and Salary
fields of the customers available in the CUSTOMERS table. To do this we must specify these three column names after the SELECT
statement. The following code shows how this is possible:
The SELECT
statement on line 28 is used to fetch the data in the specified columns.
Now let’s fetch all of the columns using * after the SELECT
clause in the CUSTOMERS table:
Quick quiz!#
Which of the following SELECT statements will display the NAME, AGE and ADDRESS columns only?
A)
SELECT *
FROM CUSTOMERS;
B)
SELECT NAME, AGE, ADDRESS
FROM CUSTOMERS;
C)
SELECT ID, NAME, AGE, ADDRESS, SALARY
FROM CUSTOMERS;
D)
SELECT NAME, AGE, ADDRESS
FROM CUSTOMERS
In the next lesson, we will discuss how to use the WHERE
clause.